//
//  WBPresentationController.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/9/22.
//

import UIKit

class WBPresentationController: UIPresentationController {
    
    // 其实就是将PopoVerViewController加到containerView上
    // MARK: - 对外提供属性
    var presentedFrame : CGRect = .zero
    
    // MARK: - 懒加载属性
    private lazy var coverView : UIView = UIView()
    
    // MARK: - 系统回调函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 1.设置弹出View的尺寸
        presentedView?.frame = presentedFrame
        
        // 2.设置蒙版试图
        setupCoverView()
        
    }
}

// MARK: - 设置UI界面相关
extension WBPresentationController {
    private func setupCoverView() {
        // 1.添加蒙版
        containerView?.insertSubview(coverView, at: 0)
        
        // 2.设置蒙版的属性
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.8)
        coverView.frame = containerView!.bounds
        
        // 3.添加手势
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(coverViewClick))
        coverView.addGestureRecognizer(tapGes)
        
    }
}

// MARK: - 事件监听
extension WBPresentationController {
    @objc private func coverViewClick() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
